<?php


namespace App\Http\Controllers\Admin;


use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class DhPlatController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth:admin');
    }

    public function index()
    {
        $list = DB::table('dh_platform')
            ->orderBy('id', 'desc')
            ->get();
        return view('admin.dhPlat.index', ['list' => $list]);
    }

    public function edit($id)
    {
        $detail = DB::table('dh_platform')->find($id);
        return view('admin.dhPlat.edit', ['detail' => $detail]);
    }

    public function update(Request $request, $id)
    {
        //验证表单
        $this->validate($request, [
            'title' => ['required','string'],
            'app_key' => ['required','string'],
            'iuserid' => ['required','string'],
        ]);
        if($id != 0){
            // 更新
            DB::table('dh_platform')
                ->where('id', $id)
                ->update([
                    'title' => $request->title,
                    'app_key' => $request->app_key,
                    'iuserid' => $request->iuserid,
                ]);
        }else{
            // 创建
            DB::table('dh_platform')
                ->insert([
                    'title' => $request->title,
                    'app_key' => $request->app_key,
                    'iuserid' => $request->iuserid,
                ]);
        }
        return successResp('');
    }

    public function destroy($id)
    {
        DB::table('dh_platform')
            ->whereIn('id', explode(',', $id))
            ->delete();
        return successResp('');
    }
}
